***********************************************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This do-file merges workplace information at individual level, individual
* info on socioeconomics and demographics, elections results and 
* collpses all on precinct level for each year 2006, 2010 and 2014
* It also adds info on labor maarket regions used for control.  
*********************************************************************

clear all

cd "D:\SCB_ConPol\Stata"
set more off

use "RTB\RTB_2006" 


duplicates drop LopNr, force /* 6 obs*/

///merge on some birth data
merge 1:m LopNr using "RTB\Fodelseuppg"

///keep only those living in the country in 2010
	keep if _merge==3
	drop _merge
	duplicates drop LopNr, force /*1 obs*/

gen year=2006

tostring FodArMan, replace
gen year_born = substr(FodArMan, 1, 4) 
destring year_born, replace

gen month_born = substr(FodArMan, 5, 2)
destring month_born, replace

gen age =year-year_born

	drop if age<18
	

///Birth country data
merge 1:1 LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3 
	drop _merge

///LISA - use year before
merge 1:m LopNr using "LISA\LISA_2005"
	keep if _merge==3 
	drop _merge

duplicates drop LopNr, force /*4 obs*/

///Workplace data
gen sample=1
drop ArbstId Sektorkod
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2006.dta"
	keep if sample==1
	drop _merge sample
	
	///Workplace data adding the robust version of share of foreign born
gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2006_rob.dta"
	keep if sample==1
	drop _merge sample

	///Valdeltagardata

merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2006_electiondistrictsperperson.dta"
	keep if _merge==3
	drop _merge 
	rename llkkdddd llkkdddd_new

	///Valdeltagardata 02
		gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2002_electiondistrictsperperson.dta"
	keep if sample==1
	drop _merge sample

	gen share_im_cworker_swe_stayed =  share_im_cworker_swe if llkkdddd== llkkdddd_new
	gen im_coworker_swe_stayed = im_coworker_swe if llkkdddd== llkkdddd_new
	gen sh_im_cw_noi_swe_stay = sh_im_cw_noi_swe if llkkdddd== llkkdddd_new
	gen coworkers_swe_stayed = coworkers_swe if llkkdddd== llkkdddd_new
	gen inflow = 1 if llkkdddd!= llkkdddd_new
	gen inflow_swe = 1 if llkkdddd!= llkkdddd_new & IFAUkod==0
	drop llkkdddd
rename llkkdddd_new llkkdddd

///migration date
merge 1:m LopNr using "RTB\Migrationer"
drop _merge
*drop those not in the country 2006
drop if Kommun==.

*Eliminate duplicates

*keep first immigration year
sort LopNr Datum 
bysort LopNr: gen n=_n

duplicates tag LopNr, gen(circleim)
tab PostTyp, gen(im_em)

	keep if im_em1==. /*not imm.*/ | (im_em1==1 & n==1) /*first immigration date*/ | (im_em1==1 & n==2) /*first immigration date if first date registeterd is emigration date*/

	///left with a number of people who for some reasons imigrated twise without emigrating
	*duplicates report LopNr
	drop circleim
	duplicates tag LopNr, gen(circleim)
	sort  circleim LopNr
	drop if n==2

	tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace


///PREPARE DATA TO COLLAPSE
gen pop =1 
replace Kon=. if Kon==2
rename Kon men

gen young = 1 if age<31


gen swedish = 1 if IFAUkod==0
gen nordic = 1 if IFAUkod==26 | IFAUkod==27 | IFAUkod==28
gen western = 1 if IFAUkod == 32 | IFAUkod == 33 | IFAUkod == 34 | IFAUkod == 38 | IFAUkod == 39
gen europe_other = 1 if IFAUkod == 29 | IFAUkod == 30 | IFAUkod == 31 | IFAUkod == 35 | IFAUkod == 36 | IFAUkod == 37
gen other =  1 if IFAUkod >39 & IFAUkod < 54

*Married

encode Civil, gen(civil)  
gen married = 1 if civil==2 | civil==4

tostring CivDat, replace
gen year_marr = substr(CivDat, 1, 4) 
destring year_marr, replace

gen new_married = 1 if married ==1 & year_marr==2006

gen divorced = 1 if civil==5
gen new_divorce = 1 if divorced ==1 & year_marr==2006


*Parent
gen parent =1 if Barn0_3>0 | Barn4_6>0 | Barn7_10>0 | Barn11_15> 0 | Barn16_17>0

encode Sun2000niva_old, gen(Sun2000niva_old_dstr)
gen high_educ = 1 if Sun2000niva_old_dstr>5
gen low_educ = 1 if Sun2000niva_old_dstr==1 | Sun2000niva_old_dstr==2


*Using Yrkstalln
gen employed = 1 if Yrkstalln==2
gen employed_nordic = 1 if Yrkstalln==2 & nordic == 1
gen employed_swedish = 1 if Yrkstalln==2 & swedish == 1
gen employed_western = 1 if Yrkstalln==2 & western == 1
gen employed_europeother = 1 if Yrkstalln==2 & europe_other == 1
gen employed_other = 1 if Yrkstalln==2 & other == 1

*self-employed
gen se = 1 if Yrkstalln==4 | Yrkstalln==5
gen se_nordic = 1 if (Yrkstalln==4 | Yrkstalln==5)  & nordic == 1
gen se_swedish = 1 if (Yrkstalln==4 | Yrkstalln==5) & swedish == 1
gen se_western = 1 if (Yrkstalln==4 | Yrkstalln==5) & western == 1
gen se_europeother = 1 if (Yrkstalln==4 | Yrkstalln==5) & europe_other == 1
gen se_other = 1 if (Yrkstalln==4 | Yrkstalln==5) & other == 1

*unemployed
gen unemployed = 1 if Yrkstalln==0
gen unemployed_nordic = 1 if Yrkstalln==0 & nordic == 1
gen unemployed_swedish = 1 if Yrkstalln==0 & swedish == 1
gen unemployed_western = 1 if Yrkstalln==0 & western == 1
gen unemployed_europeother = 1 if Yrkstalln==0 & europe_other == 1
gen unemployed_other = 1 if Yrkstalln==0 & other == 1

*Skill composition of non-European immigrants
gen other_high_educ = 1 if other ==  1 & high_educ == 1 
gen other_low_educ = 1 if other ==  1 & low_educ == 1 


*Citizens: 
encode Medblandnamn, gen(medb)
gen ctz = 1 if medb==10 /*Sweden is 10*/
gen ctz_other = 1 if medb==10 & other==1
*new citizens
tostring MedBdat, gen(date_ctz)
gen year_ctz = substr(date_ctz, 1, 4) 
destring year_ctz, replace
gen new_ctz = 1 if year_ctz==2006 & ctz==1 & UtlSvBakG==11
gen im = 1 if UtlSvBakG==11

///high and low skilled use first digit in SSYK
capture gen SSKY_str = substr(SSYK3, 1,1)
capture gen SSKY_str = substr(ssyk3_2012, 1,1)
encode SSKY_str, gen(SSKY1)
gen high_skilled_nh=1 if SSKY1==1 | SSKY1==2 | SSKY1==3
gen low_skilled_nh =1 if SSKY1>3 & SSKY1!=. 


*Just to count the actual used number of individuals
preserve
gen S=1
keep LopNr S
save  "E:\ProjData\IntegrationSD\temp\2006_sampleofind.dta", replace
restore 

///collapse

collapse (count) pop young men swedish im nordic western europe_other other married parent ///
high_educ low_educ ctz ctz_other unemployed unemployed_nordic unemployed_swedish ///
unemployed_western unemployed_europeother unemployed_other other_high_educ ///
other_low_educ workers workers_swe inflow inflow_swe divorced new_divorce ///
employed employed_nordic employed_swedish employed_western employed_europeother /// 
employed_other se se_nordic se_swedish se_western se_europeother se_other /// 
highsk_other lowsk_other high_skilled low_skilled high_skilled_nh low_skilled_nh new_ctz new_married ///
(mean) LoneInk DispInk04 ALosDag SocBidrPersF04 im_coworker coworkers  ///
share_im_cworker share_im_cworker_swe im_coworker_swe deviation_sector ///
share_im_cworker_swe_stayed share_matched_swe_smwp share_matched_swe_bigwp ///
im_coworker_swe_stayed coworkers_swe_stayed coworkers_swe ///
latin_coworker mena_coworker asia_coworker africa_coworker rest_coworker nordic_coworker western_coworker ///
share_latin_cworker share_latin_cworker_swe latin_coworker_swe share_mena_cworker /// 
share_mena_cworker_swe mena_coworker_swe share_asia_cworker share_asia_cworker_swe ///
asia_coworker_swe share_africa_cworker share_africa_cworker_swe africa_coworker_swe ///
share_rest_cworker share_rest_cworker_swe rest_coworker_swe share_nordic_cworker ///
share_nordic_cworker_swe nordic_coworker_swe share_western_cworker ///
share_western_cworker_swe western_coworker_swe yearincountry  ///
newim_coworker mediumnewim_coworker notnewim_coworker oldim_coworker /// 
share_newim_cworker share_newim_cworker_swe newim_coworker_swe ///
share_mediumnewim_cworker share_mediumnewim_cworker_swe mediumnewim_coworker_swe ///
share_notnewim_cworker share_notnewim_cworker_swe notnewim_coworker_swe ///
share_oldim_cworker share_oldim_cworker_swe oldim_coworker_swe ///
highsk_coworker lowsk_coworker highskother_coworker lowskother_coworker  ///
share_highsk_cworker share_highsk_cworker_swe highsk_coworker_swe share_lowsk_cworker ///
share_lowsk_cworker_swe lowsk_coworker_swe share_highskother_cworker share_highskother_cworker_swe ///
highskother_coworker_swe share_lowskother_cworker share_lowskother_cworker_swe ///
sector_share_im lowskother_coworker_swe matchedcoworkers share_matchedhigh_cworker ///
share_matchedlow_cworker sharecrossskill_highnative sharecrossskill_lownative crossskill ///
matchedcoworkers_all share_matchedhigh_allcworker share_matchedlow_allcworker ///
share_im_cworker_swe_smwp share_im_cworker_swe_bigwp share_cross_bigwp share_cross_smallwp ///
share_young_cw_wp share_male_cw_wp wage_cw_wp share_ssyk2 ///
share_ssyk2_topRTI share_ssyk2_bottomRTI share_ssyk2_midbottomRTI ///
share_non_ssyk2 share_ssyk2_midtopRTI share_ssyk2_big stdRTI ///
 share_ssyk2_small share_non_ssyk2_big share_non_ssyk2_small matchedcw ///
sh_im_cw_noi_swe_stay sh_im_cw_noi_swe share_topRTI share_ssyk1 share_non_ssyk1 ///
share_midtopRTI share_midbottomRTI share_bottomRTI ///
 share_matched_big share_matched_small share_cross_big share_cross_small share_matched_topRTI ///
 share_cross_topRTI share_matched_bottomRTI share_cross_bottomRTI sh_im_cw_noi_swe_rob , by(llkkdddd)
 
rename other other_im  
 
	///Valresultat-data

*merge 1:1 llkkdddd using "D:\Data\ProjData\IntegrationSD\temp\2006_electionresults.dta"
*keep if _merge==3 
	*drop _merge 
	
save "E:\ProjData\IntegrationSD\temp\2006.dta", replace

	
********
* 2010 *
********

clear all

cd "D:\SCB_ConPol\Stata"

use "RTB\RTB_2010" 


duplicates drop LopNr, force /* 44 obs*/

///merge on some birth data
merge 1:m LopNr using "RTB\Fodelseuppg"

///keep only those living in the country in 2010
	keep if _merge==3
	drop _merge
	duplicates drop LopNr, force /*1 obs*/

gen year=2010

tostring FodArMan, replace
gen year_born = substr(FodArMan, 1, 4) 
destring year_born, replace

gen month_born = substr(FodArMan, 5, 2)
destring month_born, replace

gen age =year-year_born

	drop if age<18
///Birth country data
merge 1:1 LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3 /*1 ind dropped*/
	drop _merge

///LISA - use year before
merge 1:m LopNr using "LISA\LISA_2009"
	keep if _merge==3 
	drop _merge

duplicates drop LopNr, force /*24 obs*/

///Workplace data
gen sample=1
drop ArbstId Sektorkod
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2010.dta"
	keep if sample==1
	drop _merge sample
	
	
		///Workplace data adding the robust version of share of foreign born
gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2010_rob.dta"
	keep if sample==1
	drop _merge sample
	
	///Valdeltagardata

merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2010_electiondistrictsperperson.dta"
	keep if _merge==3
	drop _merge 
	
		rename llkkdddd llkkdddd_new

	///Valdeltagardata 06
		gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2006_electiondistrictsperperson.dta"
	keep if sample==1
	drop _merge sample

	gen share_im_cworker_swe_stayed =  share_im_cworker_swe if llkkdddd== llkkdddd_new
	gen im_coworker_swe_stayed = im_coworker_swe if llkkdddd== llkkdddd_new
		gen sh_im_cw_noi_swe_stay = sh_im_cw_noi_swe if llkkdddd== llkkdddd_new
	gen coworkers_swe_stayed = coworkers_swe if llkkdddd== llkkdddd_new
	gen inflow = 1 if llkkdddd!= llkkdddd_new
	gen inflow_swe = 1 if llkkdddd!= llkkdddd_new & IFAUkod==0
	drop llkkdddd
rename llkkdddd_new llkkdddd

///migration date
merge 1:m LopNr using "RTB\Migrationer"
drop _merge
*drop those not in the country 2010
drop if Kommun==.

*Eliminate duplicates

*keep first immigration year
sort LopNr Datum 
bysort LopNr: gen n=_n

duplicates tag LopNr, gen(circleim)
tab PostTyp, gen(im_em)

	keep if im_em1==. /*not imm.*/ | (im_em1==1 & n==1) /*first immigration date*/ | (im_em1==1 & n==2) /*first immigration date if first date registeterd is emigration date*/

	///left with a number of people who for some reasons imigrated twise without emigrating
	*duplicates report LopNr
	drop circleim
	duplicates tag LopNr, gen(circleim)
	sort  circleim LopNr
	drop if n==2

	tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace


///PREPARE DATA TO COLLAPSE
gen pop =1 
replace Kon=. if Kon==2
rename Kon men

gen young = 1 if age<31

gen swedish = 1 if IFAUkod==0
gen nordic = 1 if IFAUkod==26 | IFAUkod==27 | IFAUkod==28
gen western = 1 if IFAUkod == 32 | IFAUkod == 33 | IFAUkod == 34 | IFAUkod == 38 | IFAUkod == 39
gen europe_other = 1 if IFAUkod == 29 | IFAUkod == 30 | IFAUkod == 31 | IFAUkod == 35 | IFAUkod == 36 | IFAUkod == 37
gen other =  1 if IFAUkod >39 & IFAUkod < 54


*Married

encode Civil, gen(civil)  
gen married = 1 if civil==2 | civil==4

tostring CivDat, replace
gen year_marr = substr(CivDat, 1, 4) 
destring year_marr, replace

gen new_married = 1 if married ==1 & year_marr==2010
gen divorced = 1 if civil==5
gen new_divorce = 1 if divorced ==1 & year_marr==2010


*parent
gen parent =1 if Barn0_3>0 | Barn4_6>0 | Barn7_10>0 | Barn11_15> 0 | Barn16_17>0

*educ
encode Sun2000niva_old, gen(Sun2000niva_old_dstr)
gen high_educ = 1 if Sun2000niva_old_dstr>5
gen low_educ = 1 if Sun2000niva_old_dstr==1 | Sun2000niva_old_dstr==2


*Using Yrkstalln
gen employed = 1 if Yrkstalln==2
gen employed_nordic = 1 if Yrkstalln==2 & nordic == 1
gen employed_swedish = 1 if Yrkstalln==2 & swedish == 1
gen employed_western = 1 if Yrkstalln==2 & western == 1
gen employed_europeother = 1 if Yrkstalln==2 & europe_other == 1
gen employed_other = 1 if Yrkstalln==2 & other == 1

*self-employed
gen se = 1 if Yrkstalln==4 | Yrkstalln==5
gen se_nordic = 1 if (Yrkstalln==4 | Yrkstalln==5)  & nordic == 1
gen se_swedish = 1 if (Yrkstalln==4 | Yrkstalln==5) & swedish == 1
gen se_western = 1 if (Yrkstalln==4 | Yrkstalln==5) & western == 1
gen se_europeother = 1 if (Yrkstalln==4 | Yrkstalln==5) & europe_other == 1
gen se_other = 1 if (Yrkstalln==4 | Yrkstalln==5) & other == 1

*unemployed
gen unemployed = 1 if Yrkstalln==0
gen unemployed_nordic = 1 if Yrkstalln==0 & nordic == 1
gen unemployed_swedish = 1 if Yrkstalln==0 & swedish == 1
gen unemployed_western = 1 if Yrkstalln==0 & western == 1
gen unemployed_europeother = 1 if Yrkstalln==0 & europe_other == 1
gen unemployed_other = 1 if Yrkstalln==0 & other == 1

*Skill composition of non-European immigrants
gen other_high_educ = 1 if other ==  1 & high_educ == 1 
gen other_low_educ = 1 if other ==  1 & low_educ == 1 

*Citizens: 
encode Medblandnamn, gen(medb)
gen ctz = 1 if medb==10 /*Sweden is 10*/
gen ctz_other = 1 if medb==10 & other==1

*new citizens
tostring MedBdat, gen(date_ctz)
gen year_ctz = substr(date_ctz, 1, 4) 
destring year_ctz, replace
gen new_ctz = 1 if year_ctz==2010 & ctz==1 & UtlSvBakG==11
gen im = 1 if UtlSvBakG==11


///high and low skilled use first digit in SSYK
capture gen SSKY_str = substr(SSYK3, 1,1)
capture gen SSKY_str = substr(ssyk3_2012, 1,1)
encode SSKY_str, gen(SSKY1)
gen high_skilled_nh=1 if SSKY1==1 | SSKY1==2 | SSKY1==3
gen low_skilled_nh =1 if SSKY1>3 & SSKY1!=. 

preserve
gen S=1
keep LopNr S
save  "E:\ProjData\IntegrationSD\temp\2010_sampleofind.dta", replace
restore 
///collapse


collapse (count) pop young men swedish im nordic western europe_other other married parent ///
high_educ low_educ ctz ctz_other unemployed unemployed_nordic unemployed_swedish ///
unemployed_western unemployed_europeother unemployed_other other_high_educ ///
other_low_educ workers workers_swe inflow inflow_swe divorced new_divorce ///
employed employed_nordic employed_swedish employed_western employed_europeother /// 
employed_other se se_nordic se_swedish se_western se_europeother se_other /// 
highsk_other lowsk_other high_skilled low_skilled high_skilled_nh low_skilled_nh new_ctz new_married ///
(mean) LoneInk DispInk04 ALosDag SocBidrPersF04 im_coworker coworkers  ///
share_im_cworker share_im_cworker_swe im_coworker_swe deviation_sector ///
share_im_cworker_swe_stayed share_matched_swe_smwp share_matched_swe_bigwp ///
im_coworker_swe_stayed coworkers_swe_stayed coworkers_swe ///
latin_coworker mena_coworker asia_coworker africa_coworker rest_coworker nordic_coworker western_coworker ///
share_latin_cworker share_latin_cworker_swe latin_coworker_swe share_mena_cworker /// 
share_mena_cworker_swe mena_coworker_swe share_asia_cworker share_asia_cworker_swe ///
asia_coworker_swe share_africa_cworker share_africa_cworker_swe africa_coworker_swe ///
share_rest_cworker share_rest_cworker_swe rest_coworker_swe share_nordic_cworker ///
share_nordic_cworker_swe nordic_coworker_swe share_western_cworker ///
share_western_cworker_swe western_coworker_swe yearincountry  ///
newim_coworker mediumnewim_coworker notnewim_coworker oldim_coworker /// 
share_newim_cworker share_newim_cworker_swe newim_coworker_swe ///
share_mediumnewim_cworker share_mediumnewim_cworker_swe mediumnewim_coworker_swe ///
share_notnewim_cworker share_notnewim_cworker_swe notnewim_coworker_swe ///
share_oldim_cworker share_oldim_cworker_swe oldim_coworker_swe ///
highsk_coworker lowsk_coworker highskother_coworker lowskother_coworker  ///
share_highsk_cworker share_highsk_cworker_swe highsk_coworker_swe share_lowsk_cworker ///
share_lowsk_cworker_swe lowsk_coworker_swe share_highskother_cworker share_highskother_cworker_swe ///
highskother_coworker_swe share_lowskother_cworker share_lowskother_cworker_swe ///
sector_share_im lowskother_coworker_swe matchedcoworkers share_matchedhigh_cworker ///
share_matchedlow_cworker sharecrossskill_highnative sharecrossskill_lownative crossskill ///
matchedcoworkers_all share_matchedhigh_allcworker share_matchedlow_allcworker ///
share_im_cworker_swe_smwp share_im_cworker_swe_bigwp share_cross_bigwp share_cross_smallwp ///
share_young_cw_wp share_male_cw_wp wage_cw_wp share_ssyk2 ///
share_ssyk2_topRTI share_ssyk2_bottomRTI share_ssyk2_midbottomRTI ///
share_non_ssyk2 share_ssyk2_midtopRTI share_ssyk2_big stdRTI ///
 share_ssyk2_small share_non_ssyk2_big share_non_ssyk2_small matchedcw ///
sh_im_cw_noi_swe_stay sh_im_cw_noi_swe share_topRTI share_ssyk1 share_non_ssyk1 ///
share_midtopRTI share_midbottomRTI share_bottomRTI ///
 share_matched_big share_matched_small share_cross_big share_cross_small share_matched_topRTI ///
 share_cross_topRTI share_matched_bottomRTI share_cross_bottomRTI sh_im_cw_noi_swe_rob , by(llkkdddd)
 
 rename other other_im  
 
	///Valresultat-data

*merge 1:1 llkkdddd using "D:\Data\ProjData\IntegrationSD\temp\2010_electionresults.dta"
*keep if _merge==3 /*Why are some lost in the process?*/
	*drop _merge 
	
save "E:\ProjData\IntegrationSD\temp\2010.dta", replace


********
* 2014 *
********

clear all

cd "D:\SCB_ConPol\Stata"

use "RTB\RTB_2014" 


duplicates drop LopNr, force /* 13 obs*/

///merge on some birth data
merge 1:m LopNr using "RTB\Fodelseuppg"

///keep only those living in the country in 2014
	keep if _merge==3
	drop _merge
	duplicates drop LopNr, force /*1 obs*/

gen year=2014

tostring FodArMan, replace
gen year_born = substr(FodArMan, 1, 4) 
destring year_born, replace

gen month_born = substr(FodArMan, 5, 2)
destring month_born, replace

gen age =year-year_born

	drop if age<18
	
///Birth country data
merge 1:1 LopNr using "RTB\FodelselandIFAUGrupp"
	keep if _merge==3 
	drop _merge

	
///LISA - use year before
merge 1:m LopNr using "LISA\LISA_2013"
	keep if _merge==3 
	drop _merge

duplicates drop LopNr, force /*25 obs*/

///Workplace data
gen sample=1
drop ArbstId Sektorkod
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2014.dta"
	keep if sample==1
	drop _merge sample
	
			///Workplace data adding the robust version of share of foreign born
gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\workplace_2014_rob.dta"
	keep if sample==1
	drop _merge sample
	
	///Valdeltagardata

merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2014_electiondistrictsperperson.dta"
	keep if _merge==3
	drop _merge 
	
		rename llkkdddd llkkdddd_new

	///Valdeltagardata 10
		gen sample=1
merge 1:1 LopNr using "E:\ProjData\IntegrationSD\temp\2010_electiondistrictsperperson.dta"
	keep if sample==1
	drop _merge sample

	gen share_im_cworker_swe_stayed =  share_im_cworker_swe if llkkdddd== llkkdddd_new
	gen im_coworker_swe_stayed = im_coworker_swe if llkkdddd== llkkdddd_new
		gen sh_im_cw_noi_swe_stay = sh_im_cw_noi_swe if llkkdddd== llkkdddd_new
	gen coworkers_swe_stayed = coworkers_swe if llkkdddd== llkkdddd_new
	gen inflow = 1 if llkkdddd!= llkkdddd_new
	gen inflow_swe = 1 if llkkdddd!= llkkdddd_new & IFAUkod==0
	drop llkkdddd
rename llkkdddd_new llkkdddd

///migration date
merge 1:m LopNr using "RTB\Migrationer"
drop _merge
*drop those not in the country 2014
drop if Kommun==.

*Eliminate duplicates

*keep first immigration year
sort LopNr Datum 
bysort LopNr: gen n=_n

duplicates tag LopNr, gen(circleim)
tab PostTyp, gen(im_em)

	keep if im_em1==. /*not imm.*/ | (im_em1==1 & n==1) /*first immigration date*/ | (im_em1==1 & n==2) /*first immigration date if first date registeterd is emigration date*/

	///left with a number of people who for some reasons imigrated twise without emigrating
	*duplicates report LopNr
	drop circleim
	duplicates tag LopNr, gen(circleim)
	sort  circleim LopNr
	drop if n==2

	tostring Datum, replace
gen im_year = substr(Datum, 1, 4) 
destring im_year, replace


///PREPARE DATA TO COLLAPSE
gen pop =1 
replace Kon=. if Kon==2
rename Kon men

gen young = 1 if age<31

gen swedish = 1 if IFAUkod==0
gen nordic = 1 if IFAUkod==26 | IFAUkod==27 | IFAUkod==28
gen western = 1 if IFAUkod == 32 | IFAUkod == 33 | IFAUkod == 34 | IFAUkod == 38 | IFAUkod == 39
gen europe_other = 1 if IFAUkod == 29 | IFAUkod == 30 | IFAUkod == 31 | IFAUkod == 35 | IFAUkod == 36 | IFAUkod == 37
gen other =  1 if IFAUkod >39 & IFAUkod < 54


*Married

encode Civil, gen(civil)  
gen married = 1 if civil==2 | civil==4

tostring CivDat, replace
gen year_marr = substr(CivDat, 1, 4) 
destring year_marr, replace

gen new_married = 1 if married ==1 & year_marr==2014
gen divorced = 1 if civil==5
gen new_divorce = 1 if divorced ==1 & year_marr==2014

*Parent
gen parent =1 if Barn0_3>0 | Barn4_6>0 | Barn7_10>0 | Barn11_15> 0 | Barn16_17>0

*Educ
encode Sun2000niva_old, gen(Sun2000niva_old_dstr)
gen high_educ = 1 if Sun2000niva_old_dstr>5
gen low_educ = 1 if Sun2000niva_old_dstr==1 | Sun2000niva_old_dstr==2


///Using Yrkstalln
gen employed = 1 if Yrkstalln==2
gen employed_nordic = 1 if Yrkstalln==2 & nordic == 1
gen employed_swedish = 1 if Yrkstalln==2 & swedish == 1
gen employed_western = 1 if Yrkstalln==2 & western == 1
gen employed_europeother = 1 if Yrkstalln==2 & europe_other == 1
gen employed_other = 1 if Yrkstalln==2 & other == 1

*self-employed
gen se = 1 if Yrkstalln==4 | Yrkstalln==5
gen se_nordic = 1 if (Yrkstalln==4 | Yrkstalln==5)  & nordic == 1
gen se_swedish = 1 if (Yrkstalln==4 | Yrkstalln==5) & swedish == 1
gen se_western = 1 if (Yrkstalln==4 | Yrkstalln==5) & western == 1
gen se_europeother = 1 if (Yrkstalln==4 | Yrkstalln==5) & europe_other == 1
gen se_other = 1 if (Yrkstalln==4 | Yrkstalln==5) & other == 1

*unemployed
gen unemployed = 1 if Yrkstalln==0
gen unemployed_nordic = 1 if Yrkstalln==0 & nordic == 1
gen unemployed_swedish = 1 if Yrkstalln==0 & swedish == 1
gen unemployed_western = 1 if Yrkstalln==0 & western == 1
gen unemployed_europeother = 1 if Yrkstalln==0 & europe_other == 1
gen unemployed_other = 1 if Yrkstalln==0 & other == 1


*Skill composition of non-European immigrants
gen other_high_educ = 1 if other ==  1 & high_educ == 1 
gen other_low_educ = 1 if other ==  1 & low_educ == 1 

*Citizens: 

encode Medblandnamn, gen(medb)
gen ctz = 1 if medb==10 /*Sweden is 10*/
gen ctz_other = 1 if medb==10 & other==1

*new citizens
tostring MedBdat, gen(date_ctz)
gen year_ctz = substr(date_ctz, 1, 4) 
destring year_ctz, replace
gen new_ctz = 1 if year_ctz==2014 & ctz==1 & UtlSvBakG==11
gen im = 1 if UtlSvBakG==11


///high and low skilled use first digit in SSYK
capture gen SSKY_str = substr(SSYK3, 1,1)
capture gen SSKY_str = substr(ssyk3_2012, 1,1)
encode SSKY_str, gen(SSKY1)
gen high_skilled_nh=1 if SSKY1==1 | SSKY1==2 | SSKY1==3
gen low_skilled_nh =1 if SSKY1>3 & SSKY1!=. 


preserve
gen S=1
keep LopNr S
save  "E:\ProjData\IntegrationSD\temp\2014_sampleofind.dta", replace
restore 
///collapse


collapse (count) pop young men swedish im nordic western europe_other other married parent ///
high_educ low_educ ctz ctz_other unemployed unemployed_nordic unemployed_swedish ///
unemployed_western unemployed_europeother unemployed_other other_high_educ ///
other_low_educ workers workers_swe inflow inflow_swe divorced new_divorce ///
employed employed_nordic employed_swedish employed_western employed_europeother /// 
employed_other se se_nordic se_swedish se_western se_europeother se_other /// 
highsk_other lowsk_other high_skilled low_skilled high_skilled_nh low_skilled_nh new_ctz new_married ///
(mean) LoneInk DispInk04 ALosDag SocBidrPersF04 im_coworker coworkers  ///
share_im_cworker share_im_cworker_swe im_coworker_swe deviation_sector ///
share_im_cworker_swe_stayed share_matched_swe_smwp share_matched_swe_bigwp ///
im_coworker_swe_stayed coworkers_swe_stayed coworkers_swe ///
latin_coworker mena_coworker asia_coworker africa_coworker rest_coworker nordic_coworker western_coworker ///
share_latin_cworker share_latin_cworker_swe latin_coworker_swe share_mena_cworker /// 
share_mena_cworker_swe mena_coworker_swe share_asia_cworker share_asia_cworker_swe ///
asia_coworker_swe share_africa_cworker share_africa_cworker_swe africa_coworker_swe ///
share_rest_cworker share_rest_cworker_swe rest_coworker_swe share_nordic_cworker ///
share_nordic_cworker_swe nordic_coworker_swe share_western_cworker ///
share_western_cworker_swe western_coworker_swe yearincountry  ///
newim_coworker mediumnewim_coworker notnewim_coworker oldim_coworker /// 
share_newim_cworker share_newim_cworker_swe newim_coworker_swe ///
share_mediumnewim_cworker share_mediumnewim_cworker_swe mediumnewim_coworker_swe ///
share_notnewim_cworker share_notnewim_cworker_swe notnewim_coworker_swe ///
share_oldim_cworker share_oldim_cworker_swe oldim_coworker_swe ///
highsk_coworker lowsk_coworker highskother_coworker lowskother_coworker  ///
share_highsk_cworker share_highsk_cworker_swe highsk_coworker_swe share_lowsk_cworker ///
share_lowsk_cworker_swe lowsk_coworker_swe share_highskother_cworker share_highskother_cworker_swe ///
highskother_coworker_swe share_lowskother_cworker share_lowskother_cworker_swe ///
sector_share_im lowskother_coworker_swe matchedcoworkers share_matchedhigh_cworker ///
share_matchedlow_cworker sharecrossskill_highnative sharecrossskill_lownative crossskill ///
matchedcoworkers_all share_matchedhigh_allcworker share_matchedlow_allcworker ///
share_im_cworker_swe_smwp share_im_cworker_swe_bigwp share_cross_bigwp share_cross_smallwp ///
share_young_cw_wp share_male_cw_wp wage_cw_wp share_ssyk2 ///
share_ssyk2_topRTI share_ssyk2_bottomRTI share_ssyk2_midbottomRTI ///
share_non_ssyk2 share_ssyk2_midtopRTI share_ssyk2_big stdRTI ///
 share_ssyk2_small share_non_ssyk2_big share_non_ssyk2_small matchedcw ///
sh_im_cw_noi_swe_stay sh_im_cw_noi_swe share_topRTI share_ssyk1 share_non_ssyk1 ///
share_midtopRTI share_midbottomRTI share_bottomRTI ///
 share_matched_big share_matched_small share_cross_big share_cross_small share_matched_topRTI ///
 share_cross_topRTI share_matched_bottomRTI share_cross_bottomRTI sh_im_cw_noi_swe_rob , by(llkkdddd)
 
 rename other other_im  
 
	///Valresultat-data

gen str8 llkkdddd2 = string(llkkdddd,"%08.0f")
drop llkkdddd
rename llkkdddd2 llkkdddd

merge 1:1 llkkdddd using "E:\ProjData\IntegrationSD\temp\2014_electionresults.dta"
keep if _merge==3
	drop _merge 
	
save "E:\ProjData\IntegrationSD\temp\2014.dta", replace




///Merge onto mapped files for the mapped districts

*2010-2014
use "E:\ProjData\IntegrationSD\temp\2010.dta"
gen str8 precinct2010 = string(llkkdddd,"%08.0f")
drop llkkdddd
*duplicates report lkfv

merge 1:m precinct2010 using "E:\ProjData\IntegrationSD\temp\vd_2010_2014_shares"
drop _merge

order precinct2010 whole_pop precinct2014 totbef share
save "E:\ProjData\IntegrationSD\temp\vd_2010_2014_shares_r", replace


*2006-2014

use "E:\ProjData\IntegrationSD\temp\2006.dta"
gen str8 precinct2006 = string(llkkdddd,"%08.0f")
drop llkkdddd
*duplicates report precinkt2006

merge 1:m precinct2006 using "E:\ProjData\IntegrationSD\temp\vd_2006_2014_shares"
drop _merge

order precinct2006 whole_pop precinct2014 totbef share
save "E:\ProjData\IntegrationSD\temp\vd_2006_2014_shares_r", replace


*******************************
* create labor market regions (used as control) *
*******************************

clear all

///import info on labor market regions in Sweden 2014
import excel "E:\ProjData\IntegrationSD\raw\Labor market regions 2014", firstrow 

keep Kod namn kod 

rename kod kom_str

drop if kom_str==.
save "E:\ProjData\IntegrationSD\temp\lmr2014", replace


